{{define "new"}}
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <title>新建</title>
        <link rel="stylesheet" href="../layui/css/layui.css">
    </head>
    <body style="background-color: #F2F2F2">
    <ul class="layui-nav" lay-filter="">
        <li class="layui-nav-item layui-this"><a href="">新增</a></li>
        <li class="layui-nav-item"><a href="/html/view">查看</a></li>
    </ul>
    <div class="layui-container" style="background-color: white;margin-top: 100px;text-align: center">
        <form class="layui-form" action="" style="margin-top: 50px">
            <div style="height: 50px"></div>
            <input type="text" name="title" lay-verify="required" placeholder="请输入标题" class="layui-input" style="margin-top: 50px;margin-bottom: 50px">
            <textarea name="text" lay-verify="required" placeholder="请输入内容" class="layui-textarea" style="margin-bottom: 50px"></textarea>
            <button type="button" class="layui-btn" lay-submit lay-filter="submit">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </form>
    </div>

    <script src="../layui/layui.js"></script>
    <script>
        layui.use(['form','element'], function () {
            var form = layui.form;
            var url = window.location.host;
            var layer = layui.layer;
            var $ = layui.jquery;
            form.on('submit(submit)', function(data){
                $.ajax({
                    url: "http://" + url + "/api/article",
                    dataType:"json",
                    contentType: "application/json",
                    data: JSON.stringify(data.field), //请求的附加参数，用json对象
                    method: 'POST',
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg(res.msg, {
                                icon: 6,
                                time: 2000,
                            }, function () {
                                window.location.href = "/html/view"
                            });
                        }
                        else {
                            layer.msg(res.msg + "\n" + res.error, {
                                icon: 5,
                                time:2000,
                            });
                        }
                    },
                    error: function () {
                        layer.msg("请求发送失败", {
                            icon: 5,
                            time:2000,
                        });
                    }
                });
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        });
    </script>
    </body>
    </html>
{{end}}